<?php

class Dispatcher
{
	
    public static function dispatch()
    {
      
      if (strpos($_SERVER['REQUEST_URI'], 'admin') > -1)
      {
        $url = str_replace(__ROOT_URL . 'admin/', '' , $_SERVER['REQUEST_URI']);
      }
      else{
        $url = str_replace(__ROOT_URL, '' , $_SERVER['REQUEST_URI']);
      }
      
      
      $url = explode('/', $url);
      
      $controller = !empty($url[0]) ? $url[0] . 'Controller' : 'indexController';
		
      $tpl_folder = !empty($url[0]) ? $url[0] : 'index';
		
      $method = !empty($url[1]) ? $url[1] : 'index';
		
      $arg = !empty($url[2]) ? $url[2] : NULL;
		
  		$cont = new $controller($controller);
  		
  		if ((int)method_exists($cont, $method) == 1) $cont->$method($arg);
  		
  		$cont->showTemplate($tpl_folder . '/' . $method . '.tpl');
    }

}

?>